﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http.OData.Query;

using Inovout.Cloud.DataStore.Models;
using Inovout.Models;
using MongoDB.Bson;

namespace Inovout.Cloud.DataStore.Services
{
    public interface IEntityService
    {
        /// <summary>
        /// 查询表的记录数
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <returns>记录数</returns>
        int GetEntityCount(string databaseName, string tableName);

        /// <summary>
        /// 查询所有的记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="properties">表的所有列</param>
        /// <returns>所有记录</returns>
        IQueryable QueryAll(string databaseName, string tableName);

        /// <summary>
        /// 根据ObjectId查询记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <returns>符合条件的记录</returns>
        BsonDocument FindOneByObjectId(string databaseName, string tableName, string primaryKey);

        // <summary>
        /// 插入记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">记录（格式：name-value）</param>
        void InsertEntity(string databaseName, string tableName, Dictionary<String, Object> entity);

        /// <summary>
        /// 批量插入记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="entities">记录集合</param>
        void InsertEntitiesBatch(string databaseName, string tableName, List<Dictionary<String, Object>> entities);

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="entity">更新后的对象结果</param>
        void UpdateEntity(string databaseName, string tableName, Dictionary<string, object> entity);

        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">被更新记录的主键</param>
        /// <param name="entity">更新后的对象结果</param>
        void UpdateEntity(string databaseName, string tableName, string primaryKey, Dictionary<String, Object> entity);

        /// <summary>
        /// Save方法是Insert和Update方法的组合。
        /// 如果ID字段有值则假定该文档是存在的并保存所有更新，否则插入一条新的文档记录。
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">主键</param>
        /// <param name="entity">记录结果</param>
        void SaveEntity(string databaseName, string tableName, Dictionary<String, Object> entity);

        /// <summary>
        /// 批量保存记录集
        /// </summary>
        /// <param name="databaseName"></param>
        /// <param name="tableName"></param>
        /// <param name="entities"></param>
        void SaveEntities(string databaseName, string tableName, List<Dictionary<string, object>> entities);

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="primaryKey">主键</param>
        void DeleteEntity(string databaseName, string tableName, string primaryKey);

        /// <summary>
        /// 根据条件删除记录
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        /// <param name="condition">条件</param>
        void DeleteEntity(string databaseName, string tableName, Dictionary<string, object> condition);
        
        /// <summary>
        /// 清空表
        /// </summary>
        /// <param name="databaseName">数据库名</param>
        /// <param name="tableName">表名</param>
        void DeleteAllEntities(string databaseName, string tableName);
    }
}
